package h0904;

import java.util.HashSet;
import java.util.LinkedList;

/**
  * @description 删除子数组的最大得分
  * @author 不知名帅哥
  * @date 2024/9/4 17:40
  * @version 1.0
*/
public class DeleteArrayMaxScore {
    public static void main(String[] args) {
        int[] nums={4,2,4,5,6};
        System.out.println(maximumUniqueSubarray2(nums));
    }
    public static int maximumUniqueSubarray(int[] nums) {
        int start=0;
        LinkedList<Integer> list = new LinkedList<>();
//        list.add(nums[0]);
        int sum=0;
        for (int end = 0; end < nums.length; end++) {
            while (!list.isEmpty()&&list.contains(nums[end])){
                list.removeFirst();
            }
            list.addLast(nums[end]);
            sum=Math.max(sum,list.stream().mapToInt(Integer::intValue).sum());
        }
        return sum;
    }
    public static int maximumUniqueSubarray2(int[] nums) {
        int start=0;
        HashSet<Integer> set=new HashSet<>();
        int sum=0;
        int maxSum=0;
        for (int end = 0; end < nums.length; end++) {
            while (!set.isEmpty()&&set.contains(nums[end])){
                set.remove(nums[start]);
                sum-=nums[start];
                start++;
            }
            set.add(nums[end]);
            sum+=nums[end];
            maxSum=Math.max(maxSum,sum);
        }
        return sum;
    }
}
